1、整合Servlet方式一
1.1、通过注解扫描完成Servlet组件的注册
在启动类中添加@ServletComponentScan注解,SpirngBoot启动时会扫描@WebServlet和@WebListener以及@WebFilter注解。
1.2、创建Servlet
1 2 3 4 5 6 7 8 9 10 11
| package com.xiezhenyu.springbootweb.servlet; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "FirstServlet",urlPatterns = "/first") public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { System.out.println("First Servlet......"); } }
|
1.3、修改启动类
在启动类中添加@ServletComponentScan注解,作用是在SpirngBoot启动时会扫描@WebServlet和@WebListener以及@WebFilter注解,并将该类实例化。
1 2 3 4 5 6 7
| @SpringBootApplication @ServletComponentScan public class SpringbootwebApplication { public static void main(String[] args) { SpringApplication.run(SpringbootwebApplication.class, args); } }
|
2、整合Servlet方式2
2.1、通过方法完成Servlet组件的注册
通过创建方法的方式完成Servlet组件的注册。
2.2、创建Servlet
1 2 3 4 5 6 7 8
|
public class SecondServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response){ System.out.println("Second Servlet......"); } }
|
2.3、创建Servlet配置类
Servlet配置类可以直接在启动类中配置,因为启动类中@SpringBootApplication注解也包含了@Configuration注解。也可以新建一个类中添加@Configuration注解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.xiezhenyu.springbootweb.config; import com.xiezhenyu.springbootweb.servlet.SecondServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class ServletConfig {
@Bean public ServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); bean.addUrlMappings("/second"); return bean; } }
|
3、整合Filter方式一
3.1、通过注解扫描完成Filter组件注册
在启动类中添加@ServletComponentScan注解,SpirngBoot启动时会扫描@WebFilter和@WebListener以及@WebServlet注解。
3.2、创建Filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.xiezhenyu.springbootweb.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException;
@WebFilter(filterName = "FirstFiltet",urlPatterns = "/first") public class FirstFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("进入First Filter。。。。。。"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("离开First Filter。。。。。。"); } @Override public void destroy() { } }
|
3.3、修改启动类
在启动类中添加@ServletComponentScan注解会自动扫描@WebFilter注解和@WebListener以及@WebServlet注解。
1 2 3 4 5 6 7
| @SpringBootApplication @ServletComponentScan public class SpringbootwebApplication { public static void main(String[] args) { SpringApplication.run(SpringbootwebApplication.class, args); } }
|
4、整合Filter方式二
4.1、通过方法完成Filter组件注册
通过创建方法的方式完成Filter组件的注册。
4.2、创建Filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.xiezhenyu.springbootweb.filter; import javax.servlet.*; import java.io.IOException;
public class SecondFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("进入Seond Filter。。。。。。"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("离开Second Filter。。。。。。"); } @Override public void destroy() { } }
|
4.3、创建Filter配置类
Filter配置类可以直接在启动类中配置,因为启动类中@SpringBootApplication注解也包含了@Configuration注解。也可以新建一个类中添加@Configuration注解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.xiezhenyu.springbootweb.config; import com.xiezhenyu.springbootweb.filter.SecondFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class FilterConfig { @Bean public FilterRegistrationBean getFilterRegistrationBean(){ FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); bean.addUrlPatterns("/second"); return bean; } }
|
5、整合Listener方式一
5.1、通过注解扫描完成Listener组件注册
在启动类中添加@ServletComponentScan注解,SpirngBoot启动时会扫描@WebServlet和@WebListener以及@WebFilter注解。
5.2、编写Listener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.xiezhenyu.springbootweb.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener;
@WebListener public class FirstListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent event){ } public void contextInitialized(ServletContextEvent event){ System.out.println("Listener..init..."); } }
|
5.3、修改启动类
Listener配置类可以直接在启动类中配置,因为启动类中@SpringBootApplication注解也包含了@Configuration注解。也可以新建一个类中添加@Configuration注解。
1 2 3 4 5 6 7
| @SpringBootApplication @ServletComponentScan public class SpringbootwebApplication { public static void main(String[] args) { SpringApplication.run(SpringbootwebApplication.class, args); } }
|
6、整合Listener方式二
6.1、通过方法完成Listener组件注册
通过创建方法的方式完成Listener组件的注册。
6.2、编写Listener
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.xiezhenyu.springbootweb.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;
public class SecondListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent event){ } public void contextInitialized(ServletContextEvent event){ System.out.println("Listener..init..."); } }
|
6.3、创建Listener配置类
Listener配置类可以直接在启动类中配置,因为启动类中@SpringBootApplication注解也包含了@Configuration注解。也可以新建一个类中添加@Configuration注解。
1 2 3 4 5 6 7 8 9 10 11
|
@Configuration public class ListenerConfig { @Bean public ServletListenerRegistrationBean getServletListenerRegistrationBean(){ ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener()); return bean; } }
|